package cn.hinglo.gameclaw.client.action;

import cn.hinglo.gameclaw.client.service.AddressService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.user.bean.Address;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @createTime: 2017/12/29
 * @author: HingLo
 * @description: 地址管理
 */
@RestController
@RequestMapping("/client/a/")
@Slf4j
public class AddressAction extends BaseController {


    private final AddressService addressService;

    @Autowired
    public AddressAction(AddressService addressService) {
        this.addressService = addressService;
    }

    /**
     * 地址列表
     *
     * @return 返回全部的用户集合
     */
    @GetMapping("list")
    public Result listAddress() {
        String userId = getUserId(request);
        List<Address> addressList = addressService.listAddressService(userId);
        return success(addressList);
    }

    /**
     * 通过地址的Id来查找地址
     *
     * @param id 地址的唯一Id
     * @return 返回地址
     */
    @GetMapping("find")
    public Result findById(String id) {
        Address address = (Address) addressService.findByIdAddress(id).getData();
        String userId = getUserId(request);
        //这一步用来校验该请求是否合法
        if (StrUtil.isNotBlank(userId) && userId.equalsIgnoreCase(address.getUserId())) {
            Map<String, Object> res = new HashMap<>(3);
            res.put("phone", address.getPhone());
            res.put("name", address.getName());
            res.put("address", address.getAddr() + " " + address.getDetailedAddr());
            return success(res);
        } else {
            return error("请求有误");
        }

    }

    /**
     * 添加地址
     *
     * @param address       地址
     * @param bindingResult 数据校验结果值
     * @return 返回添加状态
     */
    @PostMapping("add")
    public Result addAddress(@Valid Address address, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            //获取错误的信息
            String errorMassage = bindingResult.getFieldError().getDefaultMessage();
            return error(errorMassage);
        }
        //调用服务层方法，添加地址
        String userId = getUserId(request);
        address.setUserId(userId);
        boolean bool = addressService.addAddressService(address);
        return bool ? success(bool) : error("地址添加失败!");
    }

    @PutMapping("update")
    public Result updateAddress(@Valid Address address, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            //获取错误的信息
            String errorMassage = bindingResult.getFieldError().getDefaultMessage();
            return error(errorMassage);
        }
        //调用服务层方法，添加地址
        String userId = getUserId(request);
        boolean bool = addressService.updateAddressService(address, userId);
        //这个传入bool 是因为客户端需要
        return bool ? success(bool) : error("地址添加失败!");
    }


    /**
     * 删除地址
     *
     * @param id 地址的id
     * @return 返回删除的结果状态
     */
    @PutMapping("delete")
    public Result deleteAddress(@RequestParam("id") Long id) {
        String userId = getUserId(request);
        boolean bool = addressService.deleteAddressService(userId, id);
        return bool ? success() : error("地址删除失败!");
    }

}
